package com.example.demo.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.example.demo.entity.GroupInfo;
import com.example.demo.entity.User;
import com.example.demo.entity.vo.GroupInput;
import com.example.demo.entity.vo.GroupOutput;
import com.example.demo.exception.CmsException;
import com.example.demo.exception.ErrorCode;
import com.example.demo.mapper.GroupInfoMapper;
import com.example.demo.mapper.UserMapper;
import com.example.demo.service.GroupInfoService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.demo.utils.RedisUtil;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author chen huilong
 * @since 2022-10-04
 */
@Service
public class GroupInfoServiceImpl extends ServiceImpl<GroupInfoMapper, GroupInfo> implements GroupInfoService {

    @Resource
    private RedisUtil redisUtil;

    @Resource
    private UserMapper userMapper;

    @Override
    public List<GroupOutput> groupByVerificationCode(String token, GroupInput input) {
        input.checkParam();
        // 验证码是否正确
        QueryWrapper<User> objectQueryWrapper = new QueryWrapper<>();
        objectQueryWrapper.eq("user_number", input.getUserNumber());
        User stu = userMapper.selectOne(objectQueryWrapper);
        if (!input.getVerificationCode().equals(stu.getVerificationCode())) {
            throw new CmsException(ErrorCode.VERIFICATION_CODE_WRONG);
        }
        if (stu.getGroupId() != null && stu.getGroupId() != 0) {
            throw new CmsException(ErrorCode.PART_IN_OTHER_GROUP);
        }
        User user = (User) redisUtil.get(token);
        GroupInfo groupInfo = new GroupInfo();
        ArrayList<Long> longs = new ArrayList<>();
        if (user.getGroupId() == null) {
            // 当前学生尚未组成队伍
            groupInfo.setCount(2);
            groupInfo.setGender(user.getGender());
            groupInfo.setStudentId1(user.getUserNumber());
            groupInfo.setStudentId2(Long.valueOf(input.getUserNumber()));
            longs.add(groupInfo.getStudentId1());
            longs.add(groupInfo.getStudentId2());
            baseMapper.insert(groupInfo);
        } else {
            groupInfo = baseMapper.selectById(user.getGroupId());
            if (groupInfo.getCount() == 4) {
                throw new CmsException(ErrorCode.PEOPLE_COUNT_GREATER_FOUR);
            }
            if (stu.getGender() != groupInfo.getGender()) {
                throw new CmsException(ErrorCode.GENDER_WRONG);
            }
            int count = groupInfo.getCount();
            longs.add(groupInfo.getStudentId1());
            longs.add(groupInfo.getStudentId2());
            if (count == 2) {
                longs.add(Long.valueOf(input.getUserNumber()));
                groupInfo.setStudentId3(Long.valueOf(input.getUserNumber()));
            } else if (count == 3) {
                longs.add(groupInfo.getStudentId3());
                longs.add(Long.valueOf(input.getUserNumber()));
                groupInfo.setStudentId4(Long.valueOf(input.getUserNumber()));
            }
            groupInfo.setCount(groupInfo.getCount() + 1);
            baseMapper.updateById(groupInfo);
        }
        // 查询队伍成员信息
        LinkedList<GroupOutput> groupOutputs = new LinkedList<>();
        for (Long num : longs) {
            GroupOutput groupOutput = new GroupOutput();
            QueryWrapper<User> qw = new QueryWrapper<>();
            qw.eq("user_number", num);
            User curUser = userMapper.selectOne(qw);
            curUser.setGroupId(groupInfo.getGroupId());
            userMapper.updateById(curUser);
            groupOutput.of(curUser);
            groupOutputs.add(groupOutput);
        }
        return groupOutputs;
    }

    @Override
    public HashMap<String, Object> getRoommateInfo(String token) {
        User userEntity = (User) redisUtil.get(token);
        System.out.println(userEntity);
        if(userEntity.getRoomId() == null) {
            throw new CmsException(ErrorCode.USER_DO_NOT_HAVE_ROOM);
        }
        GroupInfo groupInfo = baseMapper.selectById(userEntity.getGroupId());
        HashMap<String, Object> stringObjectHashMap = new HashMap<>();
        stringObjectHashMap.put("roomId", userEntity.getRoomId());
        LinkedList<GroupOutput> groupOutputs = new LinkedList<>();
        ArrayList<Long> longs = new ArrayList<>();
        longs.add(groupInfo.getStudentId1());
        longs.add(groupInfo.getStudentId2());
        longs.add(groupInfo.getStudentId3());
        longs.add(groupInfo.getStudentId4());
        for(int i = 0; i < longs.size(); i++) {
            if(longs.get(i) != null) {
                QueryWrapper<User> qw = new QueryWrapper<>();
                qw.eq("user_number", longs.get(i));
                GroupOutput groupOutput = new GroupOutput();
                groupOutput.of(userMapper.selectOne(qw));
                groupOutputs.add(groupOutput);
            }
        }
        stringObjectHashMap.put("group list", groupOutputs);
        return stringObjectHashMap;
    }
}
